package com.org.core.container;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.org.core.constant.Pager;
import com.org.core.utils.ClassUtil;
import com.org.core.utils.SqlUtil;

public class ObjectSqlContainer {
	private static Log log = LogFactory.getLog(ObjectSqlContainer.class);
	public static Map<String, String> insertMap = new HashMap<String, String>();
	public static Map<String, String> updateMap = new HashMap<String, String>();
	public static Map<String, String> selectMap = new HashMap<String, String>();
	public static Map<String, String> classNameTableName = new HashMap<String, String>();
	private ObjectSqlContainer(){}

	public static void saveInsertMap(String className, String sql) {
		insertMap.put(className, sql);
	}
	public static void saveSelectMap(String className, String sql) {
		selectMap.put(className, sql);
	}

	public static String getUpdateSql(String className){
		String key = className + "UpdateSql";
		String sql = null;
		if(!updateMap.containsKey(key)){
			try {
				Class<?> clazz = Class.forName(className);
				String tableName = getTableName(className);
				updateMap.put(key, SqlUtil.createUpdateSqlBy(clazz, tableName));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		sql = updateMap.get(key);
		log.info("ObjectSqlContainer getUpdateSql (String className) =====> " +sql);
		return sql;
	}
	
	public static String getInsertSql(String className){
		String key = className + "InsertSql";
		if(!insertMap.containsKey(key)){
			try {
				Class<?> clazz = Class.forName(className);
				String tableName = getTableName(className);
				insertMap.put(key, SqlUtil.createInsertSqlBy(clazz, tableName));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		String sql = insertMap.get(key);
		log.info("ObjectSqlContainer getInsertSql (String className) =====> " +sql);
		return sql;
	}

	/**
	 * select * from xxx where xxx order by xxx;
	 * 该方法慎用: 因为是带参数的, 所以每次都是重新生成sql, 开销大
	 * @param className
	 * @param params : {表字段名(String): 查询字段值(Object)}
	 * @return
	 */
	public static String getSelectSql(String className, Map<String, Object> params) {
		String tableName = getTableName(className);
		String sql = SqlUtil.createSelectSqlBy(tableName, params);
		log.info("ObjectSqlContainer getSelectSql (String className, Map<String, Object> params) =====> " +sql);
		return sql;
	}
	
	/**
	 * select * from xxx where xxx order by xxx;
	 * 该方法慎用: 因为是带参数的, 所以每次都是重新生成sql, 开销大
	 * @param className
	 * @param params : {表字段名(String): 查询字段值(Object)}
	 * @return
	 */
	public static String getSelectSqlByPager(String className, Pager pager) {
		String tableName = getTableName(className);
		String sql = SqlUtil.createSelectSqlByPager(tableName, pager);
		log.info("ObjectSqlContainer getSelectSql(String className, Pager pager) =====> " +sql);
		return sql;
	}
	
	/**
	 * select * from xxx;
	 * @param className
	 * @param params : {表字段名(String): 查询字段值(Object)}
	 * @return
	 */
	public static String getSelectSql(String className) throws ClassNotFoundException{
		String sql = null;
		if(!selectMap.containsKey(className)){
			String tableName = getTableName(className);
			sql = SqlUtil.createSelectSqlBy(tableName, null);
			selectMap.put(className, sql);
		}

		log.info("ObjectSqlContainer getSelectSql(String className)=====> " +sql);
		return selectMap.get(className);
	}
	
	/**
	 * select * from xxx;
	 * @param className
	 * @param params : {表字段名(String): 查询字段值(Object)}
	 * @return
	 */
	public static String getDeleteSql(String className) throws ClassNotFoundException{
		String sql = null;
		if(!selectMap.containsKey(className)){
			String tableName = getTableName(className);
			sql = SqlUtil.createDeleteSqlBy(tableName, null);
			selectMap.put(className, sql);
		}
		
		log.info("ObjectSqlContainer getSelectSql(String className)=====> " +sql);
		return selectMap.get(className);
	}
	
	/**
	 * select * from xxx;
	 * @param className
	 * @param params : {表字段名(String): 查询字段值(Object)}
	 * @return
	 */
	public static String getDeleteSql(String className, Map<String, Object> params) {
		String tableName = getTableName(className);
		String sql = SqlUtil.createDeleteSqlBy(tableName, params);
		log.info("ObjectSqlContainer getDeleteSql(String className, Map<String, Object> params)=====> " +sql);
		return sql;
	}
	
	private static String getTableName(String className){
		String tableName = null;
		if(!classNameTableName.containsKey(className)) {
			try {
				Class<?> clazz = Class.forName(className);
				tableName = ClassUtil.readTableNameByAnnotation(clazz);
				classNameTableName.put(className, tableName);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		tableName = classNameTableName.get(className);
//		String sql = SqlUtil.createSelectSqlBy(tableName, null);
//		selectMap.put(className, sql);
		return tableName;
	}
}
